<?php
defined('BASEPATH') or die('forbidden');

class pullrolelevel_controller extends Controller {

    private $inc;
    private $area_list;
    private $server_list;
    
    function _init() {
        check_level(50, segment(0), segment(1), segment(2, 'index'));

        $this->inc=$this->config['inc'];

        $this->area_list=array();
        $this->server_list=get_server_list();
        foreach ($this->server_list as $k=>$v){
            $this->area_list[$v['SvrOutAddr']] = $v['SvrID']."-".$v['SvrName'][1];
        }
    }

    function index() {
        //print_r($_SESSION['gkl']);
        $areahostArray=array();
        foreach ($this->server_list as $k=>$v){
            $areahostArray[$v['host']] = $v['id']."-".$v['name'];
        }
        $selectedArea = $_SESSION['area_selected_id'];

        $this->view->assign("arealist", $this->form->select("area_selected_id", $areahostArray, $selectedArea, array('id' => 'area_selected_id')));
        $this->view->display("seal/pullrolelevel/index");
    }

    function flexigrid(){
        list($colkey, $colsinfo, $where, $sortname, $sortorder, $offset, $rp, $page) = $this->common->get_flexigrid_params();
        $rows = array();
        $sql = "SELECT COUNT(*) AS num FROM {pre}admin_log WHERE  modulename like '修改角色数据'";
        $one = $this->db->one($sql);
        $total = $one['num'];
        $orderby = $sortname ? "ORDER BY {$sortname} {$sortorder} " : "";
        $this->db->query("SELECT * FROM {pre}admin_log WHERE  modulename like '修改角色数据' {$orderby} LIMIT {$offset},{$rp}");
        while ($n = $this->db->get_array('default', MYSQL_ASSOC)) {
            $content = $result=json_decode($n['content'],true);
            $values['role_id'] = $content['after']["id"];
            $values['role_level'] = $content['after']["level"];
            $values['role_exp'] = $content['after']["exp"];
            $values['vip_exp'] = $content['after']["vip_exp"];

            $action_str = "";
            $i = 0;
            foreach ($content['after']["item"] as $avalue) {
                # code...
                $action_str = $action_str . "[ " . $avalue . ", " . $content['after']['amount'][$i] . "] ";
                $i = $i + 1;
            }
            $values['sealactions'] = $action_str;

            $money_str = "";
            $i = 0;
            foreach ( $content['after']["money_item"] as  $mvalue) {
                # code...
                $money_str = $money_str . "[ " . $mvalue . ", " . $content['after']['money_amount'][$i] . "] ";
                $i = $i + 1;
            }
            $values['sealmoneys'] = $money_str;

            $rows[] = array('id' => $n['id'], 'cell' => array_values($values));
        }
        $result = array(
            'page' => $page,
            'total' => $total,
            'rows' => $rows
        );
        echo json_encode($result);
    }


    function add($id = 0) {
        echo $_SESSION['EYO'];
        if (is_submit()) {
            $rowdata = $_POST['row'];
            var_dump($rowdata);

            $post_data['SessionID']="ewewqw";
            $post_data['SessionKey']="wqqew";
            $post_data['TargetID']=(int)$rowdata['id'];
            $post_data['Level']=(int)$rowdata['level'];
            //$post_data['VipLevel']=(int)$rowdata['vip_level'];
            $post_data['VipExp']=(int)$rowdata['vip_exp'];
            $post_data['Exp']=(int)$rowdata['exp'];
            foreach ($rowdata['item'] as $k=>$v){
                $item=array("ID"=>(int)$v,"Num"=>(int)$rowdata['amount'][$k]);
                $post_data['Actions'][$k]=$item;
            }
            foreach ($rowdata['money_item'] as $k=>$v){
                $item=array("ID"=>(int)$v,"Num"=>(int)$rowdata['amount'][$k]);
                $post_data['Moneys'][$k]=$item;
            }
            var_dump(json_encode($post_data));

            $area_host=$rowdata['area_host'];
//            unset($rowdata['area_host']);
//            $_SESSION['role_name']=$rowdata['role_name'];
            $_SESSION['area_host']=$area_host;

            $strResult = vpost($area_host.'/gm_set_player_info',3,json_encode($post_data));
            $_SESSION['EYO']=$strResult;

            $result=json_decode($strResult,true);
            if($result['RetCode'] == 0)
            {
                $rowdata['insert_time']=get_datetime(time());
                $arealist=get_area_list_host();
                $rowdata['area_name']=$arealist[$area_host];
                $rowdata['area_host']=$area_host;
                unset($rowdata['name']);
                insert_log(translate('MENU_SEAL_PULLROLELEVEL'),translate('ADD'),NULL, $rowdata);
                cp_tips("success");
            }
            else  //操作失败或者连接失败
            {
                cp_tips("error");
            }
        }


        $this->view->assign('role_name', $_SESSION['role_name']);
        $this->view->assign("arealist", $this->form->select("row[area_host]", $this->area_list, $_SESSION['area_host'], array('id' => 'area_host')));
        $this->view->display("seal/pullrolelevel/add");
    }
}

?>
